home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / KeyedCollection.st < prev    next >
Text File  |  2000-02-13  |  2KB  |  86 lines

  1. Class KeyedCollection :Collection
  2. [
  3.    add: anElement
  4.       ^ self error: 'Must add with explicit key'
  5. |
  6.    addAll: aCollection
  7.       aCollection binaryDo: [:x :y | self at: x put: y].
  8.       ^ aCollection
  9. |
  10.    asDictionary        ! newCollection !
  11.       newCollection <- Dictionary new.
  12.       self binaryDo: 
  13.          [:key :val | newCollection at: key put: val].
  14.  
  15.       ^ newCollection
  16. |
  17.    at: key
  18.       ^ self at: key ifAbsent:
  19.                  [self error:
  20.                      (key printString , ': association not found').
  21.       ^ key]
  22. |
  23.    atAll: aCollection put: anObject
  24.       aCollection do: [:x | self at: x put: anObject]
  25. |
  26.    binaryDo: aBlock  ! item !
  27.       self do: [:x | aBlock value: self currentKey value: x ].
  28.       ^ nil
  29. |
  30.    collect: aBlock 
  31.       ^ self coerce:
  32.            (self inject: Dictionary new
  33.                    into: [:x :y | x at: self currentKey
  34.                                    put: (aBlock value: y) . x ] )
  35. |
  36.    includesKey: key
  37.       self at: key ifAbsent: [^ false].
  38.       ^ true
  39. |
  40.    indexOf: anElement
  41.       ^ self indexOf: anElement
  42.       ifAbsent: [self error: 'indexOf element not found']
  43. |
  44.    indexOf: anElement ifAbsent: exceptionBlock
  45.       self do: [:x | (x = anElement) 
  46.                 ifTrue: [ ^ self currentKey ]].
  47.       ^ exceptionBlock value
  48. |
  49.    keys ! newset !
  50.       newset <- Set new.
  51.       self keysDo: [:x | newset add: x].
  52.       ^ newset
  53. |
  54.    keysDo: aBlock
  55.       ^ self do: [ :x | aBlock value: self currentKey ]
  56. |
  57.    keysSelect: aBlock          
  58.       ^ self coerce:
  59.            (self inject: Dictionary new
  60.                    into: [:x :y | (aBlock value: y currentKey)
  61.                                   ifTrue: [x at: self currentKey
  62.                                             put: y]. x ] )
  63. |
  64.    remove: anElement
  65.       ^ self error: 'object must be removed with explicit key'
  66. |
  67.    removeKey: key
  68.       ^ self removeKey: key ifAbsent:
  69.              [self error: 'no element associated with key'. ^ key]
  70. |
  71.    removeKey: key ifAbsent: exceptionBlock
  72.       ^ self error: 'subclass should implement RemoveKey:ifAbsent:'
  73. |
  74.    select: aBlock          
  75.       ^ self coerce:
  76.            (self inject: Dictionary 
  77.                    into: [:x :y | (aBlock value: y)
  78.                                   ifTrue: [x at: self currentKey
  79.                                             put: y]. x ] )
  80. |
  81.    values       ! newbag !
  82.       newbag <- Bag new.
  83.       self do: [:x | newbag add: x].
  84.       ^ newbag
  85. ]
  86.